//
//  XDPlaceDetailsViewController.m
//  Le way walkers
//
//  Created by YaZhiLiang on 15/5/14.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPlaceDetailsViewController.h"

@interface XDPlaceDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UIImageView *gifImageView;

@property (nonatomic, retain) UIView *gifView;

@end

@implementation XDPlaceDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createViews];
    self.collectionView.bounces = NO;
    
    
    [self yangTuo];
    
}

- (void)getData
{
    NSLog(@"%@", self.num);
    [NetworkHandle getDataWithURL:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%@/", self.num] completion:^(id result) {
        
        NSLog(@"wowowowowowow%@", self.num);
        
        self.title = [result objectForKey:@"title"];
       
        NSMutableArray *arrr = [result objectForKey:@"data"];
        
        self.arr = [NSMutableArray array];
        
        for (NSDictionary *dic in arrr) {
            NSLog(@"%@", dic);
            XDDetailsCollectionViewModel *details = [[XDDetailsCollectionViewModel alloc]initWithDic:dic];
            
            [self.arr addObject:details];
            
            NSLog(@"122312324%@", details.cover_s);
            
            [details release];
            
        }
        
        [self.collectionView reloadData];
        
        [self.gifImageView stopAnimating];
        
        self.gifView.hidden = YES;
        
    }];
}

- (void)yangTuo
{
    [self getData];
    self.gifImageView.hidden = NO;
    self.gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 - 50, 80, 100)];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"cao1.png"],
                         [UIImage imageNamed:@"cao2.png"],
                         [UIImage imageNamed:@"cao3.png"],
                         [UIImage imageNamed:@"cao4.png"],
                         [UIImage imageNamed:@"cao5.png"],
                         [UIImage imageNamed:@"cao6.png"],
                         [UIImage imageNamed:@"cao7.png"],
                         [UIImage imageNamed:@"cao8.png"],
                         [UIImage imageNamed:@"cao9.png"],
                         [UIImage imageNamed:@"cao10.png"],nil];
    self.gifImageView.animationImages = gifArray; //动画图片数组
    self.gifImageView.animationDuration = 0.8; //执行一次完整动画所需的时长
    self.gifImageView.animationRepeatCount = 10000;  //动画重复次数
    self.gifView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    UIColor *imageB = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    self.gifView.backgroundColor = imageB;
    [self.gifView addSubview:self.gifImageView];
    [self.gifImageView startAnimating];
    [self.view addSubview:self.gifView];
    [_gifImageView release];
    [_gifView release];
}

- (void)createViews
{
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowlayout];
    
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    UIColor *image1 = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    
    self.collectionView.backgroundColor = image1;
    
    flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 60) / 2, (self.view.frame.size.width - 60) / 2);
    
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    flowlayout.sectionInset = UIEdgeInsetsMake(20, 20, 0, 20);
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 113, 0);
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    
    [flowlayout release];
    
    [self.collectionView registerClass:[XDDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
//    self.tabBarController.tabBar.hidden = YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-xiangzuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
}

- (void)back:(UIBarButtonItem *)back
{
    [self.navigationController popViewControllerAnimated:YES];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"55555555%ld", self.arr.count);
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XDDetailsCollectionViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    XDDetailsCollectionViewModel *xdDetails = self.arr[indexPath.row];
    
    cell.xdDeails = xdDetails;
    
    NSLog(@"0000000000%@", xdDetails);
    
    return cell;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    XDPlaceScenicDetailsViewController *scenicVC = [[[XDPlaceScenicDetailsViewController alloc]init] autorelease];
    
    
    XDDetailsCollectionViewCell *cell = (XDDetailsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    scenicVC.ScenicID = cell.ScenicID;
    
    scenicVC.typeName = cell.typeName;
    
    NSLog(@"9999999-----------%@", cell.ScenicID);
    
    [self.navigationController pushViewController:scenicVC animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
